﻿using SienSchoofsProject.Models;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;

namespace SienSchoofsProject.Controllers
{
    [Authorize(Roles = "admin")]
    public class AdminController : BaseController
    {
        private FilmDb _db = new FilmDb();

        //
        // GET: /Admin/

        public ActionResult Index()
        {
            try
            {
                var Model = _db.UserProfiles.ToList().OrderBy(r => r.UserName);
                return View(Model);
            }
            catch (Exception e)
            {
                //Server.MapPath("~") = de root-map van het project
                string pad = Server.MapPath("~") + "/Logging/Log.txt";
                if (!System.IO.File.Exists(pad))
                {
                    System.IO.File.Create(pad);
                }

                using (StreamWriter swTracing = System.IO.File.AppendText(pad))
                {
                    swTracing.Write("\n\nDatum/Tijd: " + DateTime.Now + "\nException:\n" + e.StackTrace);
                }
            }

            return View();
        }

        [HttpPost]
        public ActionResult ChangeRole(string username)
        {
            try
            {
                var model = Roles.GetRolesForUser(username);
                TempData["RolesModel"] = model;
                TempData["User"] = username;
                return RedirectToAction("Index", "ChangeRole");
            }
            catch (Exception e)
            {
                //Server.MapPath("~") = de root-map van het project
                string pad = Server.MapPath("~") + "/Logging/Log.txt";
                if (!System.IO.File.Exists(pad))
                {
                    System.IO.File.Create(pad);
                }

                using (StreamWriter swTracing = System.IO.File.AppendText(pad))
                {
                    swTracing.Write("\n\nDatum/Tijd: " + DateTime.Now + "\nException:\n" + e.StackTrace);
                }
            }

            return View();
        }
    }
}
